﻿Imports System.Data.SqlClient
Public Class Scheduler
    Dim CN As New SqlConnection
    Public Sub generateSchedule()
        Dim studentRoster As New Dictionary(Of String, Student)
        Dim courses As Dictionary(Of String, Integer) = Nothing
        studentRoster = createStudentRoster()
        courses = possibleCoursesList(studentRoster)

        For Each c As KeyValuePair(Of String, Integer) In courses
            Console.WriteLine(c.Key & " -- " & c.Value.ToString)
        Next c

    End Sub
    Private Function createStudentRoster() As Dictionary(Of String, Student)
        'WB - Creates the connection to the Database to pull student information
        CN.ConnectionString = My.Settings.IT308ConnectionString
        Dim studentRoster As New Dictionary(Of String, Student)
        Dim CurrStudentID As String = ""
        Dim cmd As New SqlCommand
        cmd.Connection = CN
        cmd.CommandText = "SELECT * From Students"
        Dim Reader As SqlDataReader = Nothing

        'WB - connects to the Database
        Try
            CN.Open()
        Catch ex As Exception
            MessageBox.Show("Cannot connect to Database - " & ex.Message, "Error")
        End Try

        'WB - reads in all of the students information and placing them as a dictionary entry in studentRoster with their ID as the key
        Try
            Reader = cmd.ExecuteReader
            While Reader.Read
                CurrStudentID = Convert.ToString(Reader.Item("ID"))
                studentRoster.Add(CurrStudentID, New Student())
                studentRoster(CurrStudentID).IdNumber = Convert.ToInt32(CurrStudentID)
                studentRoster(CurrStudentID).FirstName = Convert.ToString(Reader.Item("FName"))
                studentRoster(CurrStudentID).LastName = Convert.ToString(Reader.Item("LName"))
                studentRoster(CurrStudentID).MiddleName = Convert.ToString(Reader.Item("MI"))
                studentRoster(CurrStudentID).Address = Convert.ToString(Reader.Item("Address1")) & vbCrLf & Convert.ToString(Reader.Item("Address2"))
                studentRoster(CurrStudentID).City = Convert.ToString(Reader.Item("City"))
                studentRoster(CurrStudentID).State = Convert.ToString(Reader.Item("State"))
                studentRoster(CurrStudentID).Zip = Convert.ToString(Reader.Item("Zip"))
                studentRoster(CurrStudentID).Program = Convert.ToString(Reader.Item("Program"))
                studentRoster(CurrStudentID).Phone = Convert.ToString(Reader.Item("Phone"))
            End While
        Catch ex As Exception
            'WB - reports errors/exceptions to the user
            MessageBox.Show("Error importing student - " & CurrStudentID & " - " & ex.Message)
        Finally
            'WB - closes the loose ends
            If CN.State = ConnectionState.Open Then CN.Close()
            Reader.Close()
        End Try
        'WB - returns the studentRoster Dictionary
        Return studentRoster
    End Function

    Private Function possibleCoursesList(ByRef studentRoster As Dictionary(Of String, Student)) As Dictionary(Of String, Integer)
        Dim courses As New Dictionary(Of String, Integer)
        Dim studentsCourses As New ArrayList

        'WB - Cycles through each student to get the their available classes.
        For Each s As KeyValuePair(Of String, Student) In studentRoster
            studentsCourses.Clear()
            studentsCourses = s.Value.getPossibleCourses()
            'WB - Cycles through all of the elements in the array list returned by the student
            For Each c In studentsCourses
                'WB - Tries to add the new course if the course is already in the array it simply adds 1 to the Integer value
                If courses.ContainsKey(c.ToString) Then
                    courses(c.ToString) += 1
                Else
                    courses.Add(c.ToString, 1)
                End If
            Next

        Next s
        Return courses
    End Function


End Class
